import 'package:flutter/material.dart';

class Student {
  String name;
  int age;
  String address;
  bool selected;

  Student({this.name, this.age, this.address, this.selected = false});
}

class WeiboFindPage extends StatefulWidget {
  @override
  _WeiboFindPageState createState() => _WeiboFindPageState();
}

class _WeiboFindPageState extends State<WeiboFindPage> {
  List<Student> list = [
    Student(name: "赵", age: 10, address: "AAA"),
    Student(name: "钱", age: 13, address: "AAA", selected: true),
    Student(name: "孙", age: 11, address: "AAA"),
    Student(name: "李", age: 15, address: "AAA"),
  ];
  bool _sortAscending = true;
  @override
  Widget build(BuildContext context) {
    List<DataColumn> columns = [
      DataColumn(label: Text('姓名'), tooltip: '长按提示'),
      DataColumn(
        label: Text('年纪'),
        tooltip: '长按提示',
        onSort: (index, sort) {
          setState(() {
            _sortAscending = sort;
            if (sort) {
              list.sort((a, b) => a.age.compareTo(b.age));
            } else {
              list.sort((a, b) => b.age.compareTo(a.age));
            }
          });
        },
      ),
      DataColumn(label: Text('地址'), tooltip: '长按提示', numeric: true),
    ];
    return Scaffold(
      appBar: AppBar(title: Text("Demo")),
      body: DataTable(
        showCheckboxColumn: true,
        sortAscending: _sortAscending,
        sortColumnIndex: 1,
        columns: columns,
        rows: buildDataRows(list),
      ),
    );
  }

  List<DataRow> buildDataRows(List<Student> data) {
    List<DataRow> rows = List();
    data.forEach((Student element) {
      rows.add(DataRow(
        selected: element.selected,
        onSelectChanged: (bool selected) {
          print(selected);
          setState(() {
            element.selected = selected;
          });
        },
        cells: [
          DataCell(
            Text(element.name),
            showEditIcon: true,
            placeholder: true, // 字体颜色会变化
            onTap: () {
              print(element.name);
            },
          ),
          DataCell(Text("${element.age}")),
          DataCell(Text(element.address)),
        ],
      ));
    });
    return rows;
  }
}
